/**
 * 
 */
package com.gkwave.action.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gkwave.module.bean.MessageBean;
import com.gkwave.module.dao.EmailDAO;
import com.gkwave.module.dao.TransportFeedbackDAO;

/**
 * @author Administrator
 *
 */
public class TransportFeedbackService extends BaseService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -762241963762803406L;

	/**
	 * 
	 */
	public TransportFeedbackService() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.gkwave.action.service.BaseService#procesRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void procesRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		out = response.getWriter();
		String idUser = request.getParameter("idUser");
		String idEmail = request.getParameter("idEmail");
		String nmEmployeeId = request.getParameter("nmEmployeeId");
		String idCity = request.getParameter("idCity");
		String txMessage = request.getParameter("txMessage");
		
		TransportFeedbackDAO dao = new TransportFeedbackDAO();
		Integer id_city = Integer.parseInt(idCity);
		boolean status = dao.writeFeedback(idEmail,idUser,id_city,txMessage,nmEmployeeId);
		
		MessageBean bean = null;
		if(status){
			String adminIdEmail = dao.getAdminEmailId(id_city);
			bean = new MessageBean("Success","Feedback sent successfully!!!");
			if(adminIdEmail!=null){
				if(adminIdEmail.length()>1){
					StringBuffer messageContent = new StringBuffer();
					messageContent.append("Feedback:");
					messageContent.append("\n==========\n");
					messageContent.append("Employee Id: " + nmEmployeeId);
					messageContent.append("\nMessage:\n");
					messageContent.append(txMessage +"\n");

					sendEmailNotification(messageContent,adminIdEmail);
				}
			}
		}else{
			bean = new MessageBean("Error","Error");
		}
		
		xmlResponse = bean.marsel(bean);
		System.out.println(xmlResponse);
		out.println(xmlResponse);
	}

	private void sendEmailNotification(StringBuffer messageContent, String idEmail) {
		// TODO Auto-generated method stub
		EmailDAO.sendEmailNotification("[SMARTC Notification] Feedback for Transport service", messageContent.toString(), new String[]{idEmail});
	}
}
